
/**
 * copyright(c) 2013 www.jiongshibaoliao.com, All Rights Reserved.
 * project: jiongshibaoliao 
 * author: 曾帅
 * date: 2014-1-9 下午05:16:06
 * version: v1.0
 */

package com.jiongshibaoliao.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiongshibaoliao.common.mybatis.Page;
import com.jiongshibaoliao.common.mybatis.PageBean;
import com.jiongshibaoliao.dao.ArticleDao;
import com.jiongshibaoliao.dao.BaseDao;
import com.jiongshibaoliao.dao.CommentDao;
import com.jiongshibaoliao.dao.MoodDao;
import com.jiongshibaoliao.dao.UserDao;
import com.jiongshibaoliao.exception.JiongException;
import com.jiongshibaoliao.model.Article;
import com.jiongshibaoliao.model.enums.Audit;
import com.jiongshibaoliao.service.ArticleService;
import com.jiongshibaoliao.util.DateUtils;

/**
 * @project jiongshibaoliao
 * @author 曾帅 
 * @version 1.0
 * @date 2014-1-9 下午05:16:06   
 */

@Service
public class ArticleServiceImpl extends BaseServiceImpl implements ArticleService {

	@Autowired
	public ArticleDao articleDao;
	
	@Autowired
	public UserDao userDao;
	
	@Autowired
	public MoodDao moodDao;
	
	@Autowired
	public CommentDao commentDao;
	
	/**
	 * 
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-1-9 下午06:02:49
	 * @see com.jiongshibaoliao.service.ArticleService#getArticlePageList(com.jiongshibaoliao.common.mybatis.PageBean, com.jiongshibaoliao.model.Article)
	 */
	private Page getArticlePageList(PageBean pageBean, Map<String, Object> map) {
		return articleDao.getArticlePageList(pageBean, map);
	}
	
	private Page getMoodMostArticlePageList(PageBean pageBean) {
		return articleDao.getMoodMostArticlePageList(pageBean);
	}

	
	/**
	 * 
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-1-16 上午11:01:58
	 * @see com.jiongshibaoliao.service.impl.BaseServiceImpl#getDao()
	 */
	@Override
	public BaseDao getDao() {
		return articleDao;
	}


	@Override
	public <T> void create(T entity) {
		Article article = (Article) entity;
		article.setAudit(Audit.UNAUDIT);
		article.setCreateDate(new Date());
		article.setLastUpdate(new Date());
		
		articleDao.create(article);
	}


	/**
	 * 
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-2-9 下午02:59:13
	 * @see com.jiongshibaoliao.service.ArticleService#getUNAuditRandom(java.lang.String, java.lang.String, long)
	 */
	@Override
	public Article getUNAuditRandom(String clientIp, Long userId) {
		int count = articleDao.getUNAuditCount(clientIp, userId);
		return articleDao.getUNAuditRandom(clientIp, userId, new Random().nextInt(count));
	}

	
	/**
	 * 
	 * @author 曾帅
	 * @version 1.0
	 * @throws JiongException 
	 * @date 2014-2-11 下午03:11:36
	 * @see com.jiongshibaoliao.service.ArticleService#auditArticle(long, com.jiongshibaoliao.model.enums.Audit)
	 */
	@Override
	public void auditArticle(long id, Audit audit) throws JiongException {
		Article article = (Article) articleDao.getEntityById(id);
		article.setAudit(audit);
		
		articleDao.modify(article);
	}

	
	/**
	 * 
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-2-11 下午10:29:02
	 * @see com.jiongshibaoliao.service.ArticleService#gethotArticlePageList(com.jiongshibaoliao.common.mybatis.PageBean)
	 */
	@Override
	public Page getHotArticlePageList(PageBean pageBean) {
		Map<String, Object> articleMap = new HashMap<String, Object>();
		articleMap.put("audit", Audit.AUDITED);
		Page page = this.getArticlePageList(pageBean, articleMap);
		
		return this.packageArticle(page);
	}


	/**
	 * 
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-2-11 下午10:34:06
	 * @see com.jiongshibaoliao.service.ArticleService#getMostArticlePageList(com.jiongshibaoliao.common.mybatis.PageBean)
	 */
	@Override
	public Page getMostArticlePageList(PageBean pageBean) {
		Page page = this.getMoodMostArticlePageList(pageBean);
		
		return this.packageArticle(page);
	}

	
	/**
	 * 
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-2-11 下午10:34:06
	 * @see com.jiongshibaoliao.service.ArticleService#getRealArticlePageList(com.jiongshibaoliao.common.mybatis.PageBean)
	 */
	@Override
	public Page getRealArticlePageList(PageBean pageBean) {
		Map<String, Object> articleMap = new HashMap<String, Object>();
		articleMap.put("audit", Audit.AUDITED);
		articleMap.put("hasImg", true);
		Page page = this.getArticlePageList(pageBean, articleMap);
		
		return this.packageArticle(page);
	}

	
	/**
	 * 
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-2-11 下午10:38:23
	 * @see com.jiongshibaoliao.service.ArticleService#getArticlePageListByUserId(com.jiongshibaoliao.common.mybatis.PageBean, long)
	 */
	@Override
	public Page getArticlePageListByUserId(PageBean pageBean, long userId) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private Page packageArticle(Page page) {
		List<Map<String, Object>> articleMapList = new ArrayList<Map<String, Object>>();
		for(Object result : page.getlistResult()) {
			Article _article = (Article) result;
			Map<String, Object> articleMap = this.getArticleMoodAndCommentCount(_article);
			articleMap.put("user", _article.getUserId() == null ? null : userDao.getEntityById(_article.getUserId()));
			
			articleMapList.add(articleMap);
		}
		page.setlistResult(articleMapList);
		
		return page;
	}
	
	private Map<String, Object> getArticleMoodAndCommentCount(Article article) {
		Map<String, Object> articleMap = new HashMap<String, Object>();
		articleMap.put("articleId", article.getId());
		articleMap.put("title", article.getTitle());
		articleMap.put("content", article.getContent());
		articleMap.put("imgPath", article.getImgPath());
		articleMap.put("issueTime", DateUtils.getInterval(article.getCreateDate()));
		articleMap.put("moodList", moodDao.getMoodByArticleId(article.getId()));
		articleMap.put("commentCount", commentDao.getCommentCountByArticleId(article.getId()));
		
		return articleMap;
	}

	
	/**
	 * 
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-2-13 下午03:51:49
	 * @see com.jiongshibaoliao.service.ArticleService#getAuditArticleRandom()
	 */
	@Override
	public Map<String, Object> getAuditArticleRandom() {
		int count = articleDao.getAuditArticleCount();
		Article article = articleDao.getAuditArticleRandom(new Random().nextInt(count));
		
		return this.getArticleMoodAndCommentCount(article);
	}

	
	/**
	 * 
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-2-17 下午02:33:02
	 * @see com.jiongshibaoliao.service.ArticleService#getArticleByArticleId(long)
	 */
	@Override
	public Map<String, Object> getPackageArticleById(long id) {
		Article article = (Article) articleDao.getEntityById(id);
		
		Map<String, Object> articleMap = this.getArticleMoodAndCommentCount(article);
		articleMap.put("user", article.getUserId() == null ? null : userDao.getEntityById(article.getUserId()));
		
		return articleMap;
	}
}
